package com.lekang.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.domain.OrderManagement;
import com.lekang.service.IOrderManagementService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author ruitong
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/orderManagement/orderManagement")
public class OrderManagementController extends BaseController
{
    @Autowired
    private IOrderManagementService orderManagementService;

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderManagement orderManagement)
    {
        System.out.println(orderManagement.toString());
        startPage();
        List<OrderManagement> list = orderManagementService.selectOrderManagementList(orderManagement);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:list')")
    @GetMapping("/listApp")
    public TableDataInfo listApp(OrderManagement orderManagement)
    {
        System.out.println(orderManagement.toString());
        startPage();
        List<OrderManagement> list = orderManagementService.selectOrderManagementListApp(orderManagement);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderManagement orderManagement)
    {
        List<OrderManagement> list = orderManagementService.selectOrderManagementList(orderManagement);
        ExcelUtil<OrderManagement> util = new ExcelUtil<OrderManagement>(OrderManagement.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderManagementService.selectOrderManagementById(id));
    }

    /**
     * 根据订单编号获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:query')")
    @GetMapping("/getOrderByOrderNO")
    public AjaxResult getInfoByOrderNO(String orderNo)
    {
        return success(orderManagementService.selectOrderManagementByOrderNO(orderNo));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderManagement orderManagement)
    {
        return toAjax(orderManagementService.insertOrderManagement(orderManagement));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderManagement orderManagement)
    {
        return toAjax(orderManagementService.updateOrderManagement(orderManagement));
    }

    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateByOrderNo")
    public AjaxResult editByOrderNo(@RequestBody OrderManagement orderManagement)
    {
        return toAjax(orderManagementService.updateOrderManagementByOrderNo(orderManagement));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderManagementService.deleteOrderManagementByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('orderManagement:orderManagement:edit')")
    @Log(title = "订单退款", businessType = BusinessType.UPDATE)
    @PutMapping("/refundByOrderNo") // 新路径，避免与支付接口冲突
    public AjaxResult refundByOrderNo(@RequestBody OrderManagement orderManagement) {
        return toAjax(orderManagementService.refundByOrderNo(orderManagement));
    }
}
